package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.vo.MemberRspVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class OrderInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRspVo> threadLocal=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/**", uri);
        boolean match1 = new AntPathMatcher().match("/payed/**", uri);
        if (match || match1){
            return true;
        }


        MemberRspVo memberRspVo= (MemberRspVo) request.getSession().getAttribute("loginUser");
        if (memberRspVo!=null){
            threadLocal.set(memberRspVo);
            return true;
        }else {
            request.getSession().setAttribute("ordererror","请先登录再下单");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
